<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>发布美景</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../plugins/mui/mui.min.css" rel="stylesheet" />
		<link href="../../css/life_housekeeper/snapshot_index.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="../../css/sweetalert/sweetalert.css"/>
		<script src="../../js/fontset.js" ></script>
		<script src="../../js/statistics.js"></script>
</head>
<body>
	<header class="mui-bar mui-bar-nav header">
	    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
	    <h1 class="mui-title">发布美景</h1>
	</header>
	<div class="mui-content">
		<div class="remark">
			<textarea placeholder="发现和记录最美的溧阳时景，说两句呗，最多可输入140个文字哦！" maxlength="140"></textarea>
			<span class="remark-statistics">(<em>0</em>/140)</span>
		</div>
		<div class="upload-img-box">
			<div class="img-default img-add">
			</div>
		</div>
		<div class="cell-item mui-navigate-right" id="search">
			<div><i class="icon icon-address"></i></div>
			<span class="mui-ellipsis"></span>
		</div>
		<div class="btn-box">
			<button class="mui-btn mui-btn-block mui-btn-warning">立即发布</button>
		</div>
	</div>
</body>
<script src="../../plugins/jquery/jquery-3.1.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../plugins/mui/mui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/sweetalert/sweetalert.min.js"></script>
<script src="../../js/getApiToken.js"></script>
<script src="../../js/md5.js" ></script>
<script src="../../js/life_housekeeper/snapshot.js" ></script>
<script src="../../js/life_housekeeper/windowTitleView.js"></script>
<script type="text/javascript">
	mui.init();
	mui.plusReady(function(){
		getGeocode();
		mui('.mui-content').on('tap','#search',function(e){
			var _this = this,
				district = _this.getAttribute('data-district'),
			 	street = _this.getAttribute('data-street');
			var viewOptions = {
				url:'snapshot-search.html',
				id:'snapshot-search'
				},
			extra = {
				street:street,
				district:district
			};
			mui.addTitleView(viewOptions,extra);
		});
		 //备注字数显示
        $(".remark textarea").on("input propertychange",function () {
            var length = $(this).val().length;
             $(".remark-statistics em").html(length);
        });
        mui('.upload-img-box').on('tap','.img-add',function(){
        	$(".remark textarea").blur();
        	checkUploadWay();
        });
        mui('.upload-img-box').on('tap','.img-close',function(){
        	$(this).parent().remove();
        	$(".upload-img-box .img-add").show();
        });
        mui('.btn-box').on('tap','button',function(){
        	$(".remark textarea").blur();
         	var userid = plus.storage.getItem('cg_user_id'),
         		content = $(".remark").children('textarea').val(),
         		address = $("#search").text().trim(),
         		img = [];
         		$(".upload-img-box img").each(function(i){
         			var datastr = this.src.split(',',3)
         			if(datastr.length>1)
					{
					img.push(datastr[1]);
					}else
					{
					img.push(datastr[0]);
					}
         		})
         		//console.log(img);
         		if(!content || content == ''){
         			mui.toast('请填写美景描述');
         			return;
         		}else if(content.length > 140){
         			mui.toast('描述不能超过140个字');
         			return;
         		}
         		if(!img.length){
         			mui.toast('请上传至少一张图片');
         			return;
         		}
         		if(!address || address == ''){
         			mui.toast('暂未获取到您的定位信息，请手动搜索您所在的位置！');
         			return;
         		}
         	var	data = {
         		userid:userid,
     			content:content,
     			address:address,
     			img:img,
     			type:1
         	};
         	plus.nativeUI.showWaiting('发布中...');
         	myAjax('home/snapshot/publish','post',data,function(e){
         		plus.nativeUI.closeWaiting();
         		//console.log(JSON.stringify(e));
         		var data = JSON.parse(e);
         		if(data.code == '1'){
         			swal({
                        title:"发布成功",
                        text: "工作人员审核通过后将在随手拍首页显示！",
                        confirmButtonText: "确认",
                        type:"success"
                    },function (e) {
                        mui.back();
                    });
         		}else{
         			swal({
                        title:'发布失败',
                        text: "请重新发布！",
                        confirmButtonText: "确认",
                        type:"error"
                    },function (e) {
                        
                    });
         		}
         	})
         	
        });
	});
		//添加newAddress自定义事件监听
	window.addEventListener('newAddress',function(event){
	  	//获得事件参数
	  	var address = event.detail.address,
	  		district = event.detail.district;
	  		$("#search span").html(address).parent().attr('data-district',district);
	});
</script>
</html>
